package com.example.backend.controller;

import com.example.backend.service.BookletService;
import com.example.backend.util.JWTUtil;
import com.example.backend.util.PageUtils;
import com.example.backend.util.Result;
import com.example.backend.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单词本相关控制器
 * @author 李月
 * @date 2022/5/2 10:46
 */
@RestController
@RequestMapping("/v1/booklets")
public class BookletController {
    @Autowired
    private BookletService bookletService;

    /**
     * 创建单词本控制器
     * @param params 参数键值对，包括用户唯一标识、单词本名称和单词本简介
     * @return 根据结果返回不同信息
     */
    @PostMapping
    public Result createBooklet(@RequestBody Map<String, Object> params){
        if(bookletService.createBooklet((int)params.get("userId"), (String)params.get("name"), (String)params.get("introduction")) == 0){
            return ResultGenerator.genSuccessResult("创建单词本成功", null);
        } else if(bookletService.createBooklet((int)params.get("userId"), (String)params.get("name"), (String)params.get("introduction")) == 1){
            return ResultGenerator.genFailResult("单词本名称不合法", null);
        } else {
            return ResultGenerator.genFailResult("单词本简介不合法", null);
        }
    }

    /**
     * 查看所有单词本控制器
     * @param userId 拥有单词本的用户id
     * @param page 页码
     * @param pageSize 页面大小
     * @return 根据结果返回不同信息
     */
    @GetMapping("/{userId}")
    public Result getBooklets(@PathVariable("userId") int userId, int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(bookletService.getBooklets(userId, page, pageSize) != null){
            PageUtils p = bookletService.getBooklets(userId, page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看所有单词本成功", map);
        } else {
            return ResultGenerator.genFailResult("查看所有单词本失败", null);
        }
    }

    /**
     * 查看单词本详情控制器
     * @param bookletId 单词本id
     * @return 单词本详情信息
     */
    @GetMapping("/booklet/{bookletId}")
    public Result getBookletSpecification(@PathVariable("bookletId") int bookletId){
        if(bookletService.getBookletSpecification(bookletId) != null){
            return ResultGenerator.genSuccessResult("查看单词本详情成功", bookletService.getBookletSpecification(bookletId));
        } else {
            return ResultGenerator.genFailResult("查看单词本详情失败", null);
        }
    }

    /**
     * 分页查看单词本条目控制器
     * @param bookletId 单词本id
     * @param page 页码
     * @param pageSize 页面大小
     * @return 单词本条目
     */
    @GetMapping("/{bookletId}/entries")
    public Result getBookletEntry(@PathVariable("bookletId") int bookletId, int page, int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(bookletService.getEntries(bookletId, page, pageSize) != null){
            PageUtils p = bookletService.getEntries(bookletId, page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看单词本条目成功", map);
        } else {
            return ResultGenerator.genFailResult("查看单词本条目失败", null);
        }
    }

    /**
     * 修改单词本详情控制器
     * @param  bookletId 单词本id
     * @param params 参数键值对，包括单词本名称、单词本简介
     * @return 根据修改情况返回结果
     */
    @PutMapping("/{bookletId}")
    public Result modifyBooklet(@PathVariable("bookletId") int bookletId, @RequestBody Map<String, Object> params){
        if(bookletService.modifyBooklet(bookletId, (String) params.get("name"), (String) params.get("introduction"))){
            return ResultGenerator.genSuccessResult("修改单词本详情成功", null);
        } else{
            return ResultGenerator.genFailResult("修改单词本详情失败", null);
        }
    }

    /**
     * 学习单词控制器
     * @param bookletId 单词本id
     * @param params 学习状态
     * @return 根据学习状态记录情况返回结果
     */
    @PutMapping("/{bookletId}/entries")
    public Result study(@PathVariable("bookletId") int bookletId, @RequestBody Map<String, List<Map<String, Object>>> params){
        if(bookletService.study(bookletId, params.get("list"))){
            return ResultGenerator.genSuccessResult("学习状态记录成功", null);
        } else {
            return ResultGenerator.genFailResult("学习状态记录失败", null);
        }
    }

    /**
     * 删除单词本条目控制器
     * @param bookletId 单词本id
     * @param params 要删除的条目id
     * @return 根据删除情况返回结果
     */
    @DeleteMapping("/{bookletId}/entries")
    public Result deleteEntry(@PathVariable("bookletId") int bookletId, @RequestBody Map<String, List<Integer>> params){
        if(bookletService.deleteEntry(bookletId, params.get("entrylist"))){
            return ResultGenerator.genSuccessResult("单词本条目删除成功", null);
        } else {
            return ResultGenerator.genFailResult("单词本条目删除失败", null);
        }
    }

    /**
     * 删除单词本控制器
     * @param bookletId 单词本id
     * @return 根据删除情况返回结果
     */
    @DeleteMapping("/{bookletId}")
    public Result deleteBooklet(@PathVariable("bookletId") int bookletId, HttpServletRequest request){
        String token = request.getHeader("token");
        int userId = Integer.parseInt(JWTUtil.getUserid(token));
//        System.out.println(userId);
        if(bookletService.deleteBooklet(userId, bookletId)){
            return ResultGenerator.genSuccessResult("单词本删除成功", null);
        } else{
            return ResultGenerator.genFailResult("单词本删除失败", null);
        }
    }

    /**
     * 增加单词本条目控制器
     * @param bookletId
     * @param params
     * @return
     */
    @PostMapping("/{bookletId}/entries")
    public Result addBookletEntry(@PathVariable("bookletId") int bookletId, @RequestBody Map<String, Object> params){
        if(bookletService.addEntry(bookletId, (int) params.get("wordId"), (String) params.get("remark")) == 0){
            return ResultGenerator.genSuccessResult("添加单词本条目成功", null);
        } else if (bookletService.addEntry(bookletId, (int) params.get("wordId"), (String) params.get("remark")) == 1) {
            return ResultGenerator.genFailResult("该条目已被添加", null);
        } else {
            return ResultGenerator.genFailResult("添加单词本条目失败", null);
        }
    }

    @GetMapping("/words/{bookletId}")
    public Result getTwentyWords(@PathVariable("bookletId") int bookletId){
        if(bookletService.getTwentyWords(bookletId) != null){
            return ResultGenerator.genSuccessResult("查询单词本条目成功", bookletService.getTwentyWords(bookletId));
        } else {
            return ResultGenerator.genFailResult("查询单词本条目失败", null);
        }
    }
}
